// 用户验证码相关操作
import { ref } from 'vue'
import { authCode } from './validate'
import { loginForm } from './loginForm'
import { getSMSsendAPI } from '@/api/sms/index'
/** 计算验证按钮是否禁用 */
export const computedDis = () => {
  const phoneExp = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
  if (countDown.value === 5 && phoneExp.test(loginForm.phone)) {
    return false
  } else {
    return true
  }
}

/** 点击按钮获取验证码 */
export const getAuthCode = async () => {
  upCountDown()
  const { data: res } = await getSMSsendAPI(loginForm.phone)
  if (res.code !== 200) return ElMessage.error(res.message)
  loginForm.authCode = res.data
  authCode.value = res.data
}

export const countDown = ref<number>(5) //倒计时
const interVal = <any>ref(null)
/** 开始倒计时 */
export const upCountDown = () => {
  interVal.value = setInterval(() => {
    if (countDown.value <= 0) {
      clearInterval(interVal.value)
      return (countDown.value = 5)
    }
    countDown.value--
  }, 1000)
}
